package com.csii.adhoc.info.menu.service.impl;

import com.csii.adhoc.dataready.TMgnDirPo;
import com.csii.adhoc.dataready.mapper.TMgnDirMapper;
import com.csii.adhoc.info.menu.mapper.TMgnMenuMapper;
import com.csii.adhoc.info.menu.service.MenuService;
import com.csii.adhoc.id.UidGenerator;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author:
 * @create:
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private UidGenerator uidGenerator;
    @Resource
    private TMgnDirMapper tMgnDirMapper;
    @Resource
    private TMgnMenuMapper tMgnMenuMapper;
    @Override
    @Transactional
    public int addMenu(TMgnDirPo tMgnDirPo) {
        //获取当前时间存入创建时间中
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        //当前日期
        String format = df.format(new Date());
        //当前用户
        String userId = String.valueOf(SecurityUtils.getSubject().getPrincipal());
        //将创建人和创建时间存入对象中
        tMgnDirPo.setCreatedBy(userId);
        tMgnDirPo.setCreatedDt(format);
        tMgnDirPo.setUpdatedBy(userId);
        tMgnDirPo.setUpdatedDt(format);
        //查数据库中排序最大值并且存入新增的对象中
        int max = tMgnMenuMapper.MenuMax();
        tMgnDirPo.setSeqno(max+1);
        //生成编号
        long uid = uidGenerator.getUID();
        tMgnDirPo.setMenuId(String.valueOf(uid));
        int add = tMgnMenuMapper.insertMenu(tMgnDirPo);
        return add;
    }

    @Transactional
    @Override
    public int deleteMenu(TMgnDirPo tMgnDirPo) {
        List list = tMgnMenuMapper.queryByParentId(tMgnDirPo.getMenuId());
        if (list.size()>0) {
            tMgnMenuMapper.deleteListMenu(list);
        }
        return tMgnMenuMapper.deleteMenu(tMgnDirPo.getMenuId());
    }

    @Override
    @Transactional
    public int editMenu(TMgnDirPo tMgnDirPo) {
        //获取当前时间存入创建时间中
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        //当前日期
        String format = df.format(new Date());
        //当前用户
        String userId = String.valueOf(SecurityUtils.getSubject().getPrincipal());
        //将创建人和创建时间存入对象中
        tMgnDirPo.setUpdatedBy(userId);
        tMgnDirPo.setUpdatedDt(format);
        return tMgnMenuMapper.updateMenu(tMgnDirPo);
    }

    @Transactional
    @Override
    public void moveMenu(TMgnDirPo tMgnDirPo) {
        int tep = 0;
        //1为上移
        if ("1".equals(tMgnDirPo.getMoveFlag())){
            //上面的对象，进行上移
            TMgnDirPo moveUp = tMgnMenuMapper.moveUp(tMgnDirPo);
            if (moveUp!=null){
                // 把旧的序号 给临时
                tep = tMgnDirPo.getSeqno();

                tMgnDirPo.setSeqno(moveUp.getSeqno());
                moveUp.setSeqno(tep);
                //存入交换后的值
                tMgnMenuMapper.updateMenu(moveUp);
                tMgnMenuMapper.updateMenu(tMgnDirPo);
            }
         //2为上移
        }else if ("2".equals(tMgnDirPo.getMoveFlag())){
            //查出下面的对象进行下移
            TMgnDirPo moveDown = tMgnMenuMapper.moveDown(tMgnDirPo);
            if (moveDown!=null){
                tep = tMgnDirPo.getSeqno();
                tMgnDirPo.setSeqno(moveDown.getSeqno());
                moveDown.setSeqno(tep);
                //交换后的值存入
                tMgnMenuMapper.updateMenu(moveDown);
                tMgnMenuMapper.updateMenu(tMgnDirPo);
            }
        }
    }

    @Override
    public List<TMgnDirPo> queryMenu() {
        return tMgnMenuMapper.queryMenu();
    }
}
